using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using Minions.Classes;
using Nsb.Classes;
using jQueryApi;

namespace MinionsEdit.Classes
{
    // ReSharper disable UnusedMember.Global
    internal class MiWidgetCss
        // ReSharper restore UnusedMember.Global
    {
        #region Css

        private const string WidgetCss = @"
.MiColorPicker {
    display: inline-block;
    margin: 0 3px;
    vertical-align: top;
}
.MiEPEnum {
    clear: both;
    position: relative;
    display: block;
}
.MiEPEnum div {
    display: inline-block;
    padding: 0 1px;
    border: 1px solid transparent;
    vertical-align: top;
}
.MiEPEnum div:hover {
    padding: 0 1px;
    border: 1px solid green;
    cursor: default;
}
.MiEPEnum div.MiEPBorderSelect {
    border: 1px solid #FFD700;
}
.MiEPSelect {
    clear: both;
    position: relative;
    display: block;
}
        ";

        #endregion Css

        static MiWidgetCss()
        {
            DomElement.AddStyle(WidgetCss);
        }
    }

    [IgnoreNamespace]
    public class MiEnumWidget : MiProperty
    {
        public MiEnumWidget(string label, string propName, int st, int en)
            : base(label, propName)
        {
            Element.AddClass("MiEPEnum");

            for (int i = st; i <= en; i++)
            {
                jQuery.FromHtml("<div/>")
                    .AppendTo(Element)
                    .Attribute("title", i.ToString())
                    .Html(i.ToString())
                    .Click(ClickEv)
                    ;
            }
        }

        protected override string WidgetValue
        {
            get { return Target.GetHtml() + Units; }
            set
            {
                int vn = Number.ParseInt(value);
                string v = Number.IsNaN(vn) ? "0" : vn.ToString();
                jQuery.Select(".MiEPBorderSelect", Element).RemoveClass("MiEPBorderSelect");
                Element.Find("div[title='" + v + "']").AddClass("MiEPBorderSelect");
            }
        }
    }

    [IgnoreNamespace]
    public class MiSelectWidget : MiProperty
    {
        protected readonly jQueryObject _sel;
        protected readonly Dictionary<string, string> _values;

        public MiSelectWidget(string label, string propName, Dictionary<string, string> values)
            : base(label, propName)
        {
            _values = values;
            DefaultValue = values.Keys[0];

            Element.AddClass("MiEPSelect");

            jQueryObject el = jQuery.FromHtml("<div/>")
                .AppendTo(Element)
                ;

            _sel = jQuery.FromHtml("<select/>")
                .AppendTo(el)
                .Change(ClickEv)
                ;

            jQuery.FromHtml("<span/>")
                .AppendTo(el)
                .Html(label)
                ;

            foreach (KeyValuePair<string, string> pair in values)
            {
                jQuery.FromHtml("<option/>")
                    .AppendTo(_sel)
                    //.Attribute("value", (i/100).ToString())
                    .Attribute("value", pair.Value)
                    .Html(pair.Key)
                    ;
            }
        }

        protected override string WidgetValue
        {
            get
            {
                string v = _sel.GetValue();
                return v == "none" ? "remove" : v;
            }
            set { _sel.Value(value ?? _values[DefaultValue]); }
        }
    }

    [IgnoreNamespace]
    public class MiColorWidget : MiProperty
    {
        public MiColorWidget(string label, string propName)
            : base(label, propName)
        {
            Element.Append(
                ColorPickerTable(label)
                    .AddClass("MiEPProp")
                    .Click(ClickEv)
                )
                ;
        }

        protected override string WidgetValue
        {
            get
            {
                jQueryObject el = jQuery.FromElement(Event.Target);
                string color = el.GetAttribute("title");
                return color == "none" ? "remove" : color;
            }
        }

        public override MiProperty Initialize(MiPropPage page)
        {
            base.Initialize(page);
            Element.Append(
                new MiAlphaWidget("Opacity", PropName)
                    .SetDefault("1")
                    .Initialize(page)
                    .Element
                );
            return this;
        }
    }

    [IgnoreNamespace]
    public class MiBorderWidthWidget : MiEnumWidget
    {
        public MiBorderWidthWidget(string label, string propName, int st, int en) : base(label, propName, st, en)
        {
        }

        protected override string LiveValue
        {
            get { return base.LiveValue; }
            set
            {
                string style = GetLive("border-style", "solid");
                style = style == "none" ? "solid" : style;
                string color = GetLive("border-color", DefaultCss["border-color"]);
                SetLive("border-style", (Number.ParseInt(value) != 0 ? style : "none"));
                SetLive("border-color", color);
                base.LiveValue = value;
                TriggerUpdate("style");
            }
        }

        public override MiProperty Initialize(MiPropPage page)
        {
            Units = "px";
            base.Initialize(page);
            return this;
        }
    }

    [IgnoreNamespace]
    public class MiBorderStyleWidget : MiSelectWidget
    {
        public MiBorderStyleWidget(string label, string propName, Dictionary<string, string> values)
            : base(label, propName, values)
        {
        }

        protected override string LiveValue
        {
            get { return base.LiveValue; }
            set
            {
                base.LiveValue = value;
                TriggerUpdate("BorderWidth");
            }
        }
    }

    [IgnoreNamespace]
    public class MiBorderRadius : MiSelectWidget
    {
        public MiBorderRadius(string label, Dictionary<string, string> values)
            : base(label, "border-radius", values)
        {
        }

        protected override string LiveValue
        {
            get
            {
                string v = Number.ParseInt(GetLive("border-radius", "0")).ToString();
                WidgetValue = v;
                return v;
            }
            set
            {
                value = value + "px";
                Dictionary d = new Dictionary(
                    "-moz-border-radius", value,
                    "-webkit-border-radius", value,
                    "border-radius", value
                    );
                SetLive("all", d);
                WidgetValue = LiveValue;
            }
        }
    }

    [IgnoreNamespace]
    public class MiAlphaWidget : MiSelectWidget
    {
        private string _color;

        public MiAlphaWidget(string label, string propName)
            : base(label, propName, AlphaValues)
        {
            jQuery.Select("select", Element).After("%&nbsp;");
        }

        protected override string LiveValue
        {
            get
            {
                _color = GetLive(PropName, "rgba(0,0,0,1)");
                WidgetValue = (new NColor().Parse(_color).Alpha).ToString();
                return WidgetValue;
            }
            // ReSharper disable ValueParameterNotUsed
            set
                // ReSharper restore ValueParameterNotUsed
            {
                // Get the live color
                _color = GetLive(PropName);
                // Change the alpha on the color to new widget value
                _color = new NColor().Parse(_color).A(Number.ParseDouble(WidgetValue)).ToRgba();
                // Set the live color with the new alpha and update the widget
                new Evo()
                    .SetValue(PropName, _color)
                    .Evoke(MinionEdit.ActiveFocus.SetCss)
                    .Response(delegate(Evo evo) { WidgetValue = (new NColor().Parse((string) evo.Value).Alpha).ToString(); })
                    .Fire();
                QueSave();
            }
        }
    }

    [IgnoreNamespace]
    public class MiFontSizeWidget : MiEnumWidget
    {
        public MiFontSizeWidget(string label, string propName, int st, int en)
            : base(label, propName, st, en)
        {
            SetUnits("autoPt");
        }

        protected override string WidgetValue
        {
            get { return Target.GetHtml(); }
            set
            {
                double vn = Number.ParseDouble(value);
                double v = Number.IsNaN(vn) ? 0 : vn;
                vn = Math.Round(v);
                jQuery.Select(".MiEPBorderSelect", Element).RemoveClass("MiEPBorderSelect");
                Element.Find("div[title='" + vn + "']").AddClass("MiEPBorderSelect");
            }
        }

        protected override string LiveValue
        {
            get
            {
                double pt = Number.ParseDouble(MinionEdit.ActiveFocus.Element.GetCSS("font-size")) * .75;
                string pts = Math.Round(pt).ToString();
                WidgetValue = pts;
                return pts;
            }
            set
            {
                double cv = Minion.Pt(MinionEdit.ActiveFocus.Element.GetCSS("font-size")); // Current visible size in pt
                double cr = Minion.Pt(GetLive("font-size")); // Current recorded size in pt
                double nv = Number.ParseDouble(value); // New visible size in pt
                string pts = Math.Round(nv/cv*cr*10)/10 + "pt";
                new Evo()
                    .SetValue(PropName, pts)
                    .Evoke(MinionEdit.ActiveFocus.SetCss)
                    .Fire();
                WidgetValue = LiveValue;
                QueSave();
            }
        }
    }

    [IgnoreNamespace]
    public class MiPxWidget : MiSelectWidget
    {
        public MiPxWidget(string label, string propName, Dictionary<string, string> values)
            : base(label, propName, values)
        {
        }

        protected override string WidgetValue
        {
            get
            {
                string v = _sel.GetValue();
                return Number.ParseInt(v) == 0 ? "remove" : v + "px";
            }
            set
            {
                value = Number.ParseInt(value).ToString();
                _sel.Value(value ?? "0");
            }
        }
    }
}